package bdhub.littletail.app.message;

import android.os.Parcel;
import android.os.Parcelable;

public class AppMessage implements Parcelable {
	private Head head;
	private String body;
	private boolean isOffline;
	public AppMessage() {
		
	}

	public Head getHead() {
		return head;
	}

	public void setHead(Head head) {
		this.head = head;
	}

	public String getBody() {
		return body;
	}

	public void setBody(String body) {
		this.body = body;
	}

	@Override
	public String toString() {
		StringBuilder buf = new StringBuilder();
		buf.append("head:").append("{").append(head.toString()).append("}")
				.append(",").append("body:").append(body);
		return buf.toString();
	}
	
	//必须提供一个名为CREATOR的static final属性 该属性需要实现android.os.Parcelable.Creator<T>接口
	public static final Parcelable.Creator<AppMessage> CREATOR = new Parcelable.Creator<AppMessage>() {

		@Override
		public AppMessage createFromParcel(Parcel source) {
			return new AppMessage(source);
		}

		@Override
		public AppMessage[] newArray(int size) {
			return new AppMessage[size];
		}
	};
	
	public AppMessage(Parcel source) {
		readFromParcel(source);
	}
	


	@Override
	public int describeContents() {
		return 0;
		//
	}
	
	/**
	 * 
	 * @param source
	 */
	public void readFromParcel(Parcel source) {
		try {
			if (source != null){
				synchronized (source) {
					Head head = new Head();
					head.setMid(source.readString());
					head.setFromId(source.readString());
					head.setToId(source.readString());
					head.setType(source.readInt());
					head.setTime(source.readLong());
					head.setComments(source.readString());
					head.setTextType(source.readInt());
					setHead(head);
					
					setBody(source.readString());
				}
			} 
		}catch(NullPointerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		Head head = getHead();
		dest.writeString(head.getMid());
		dest.writeString(head.getFromId());
		dest.writeString(head.getToId());
		dest.writeInt(head.getType());
		dest.writeLong(head.getTime());
		dest.writeString(head.getComments());
		dest.writeInt(head.getTextType());
		
		dest.writeString(getBody());
	}

	public boolean isOffline() {
		return isOffline;
	}

	public void setOffline(boolean isOffline) {
		this.isOffline = isOffline;
	}
	
	
}
